﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Business;

namespace TicketManagement.Ticket
{
    public partial class Processing : System.Web.UI.Page
    {
        private TicketBus bus;
        protected void Page_Load(object sender, EventArgs e)
        {
            bus = new TicketBus();
            if (!IsPostBack)
            {
                this.loadData();
            }
        }

        public void loadData()
        {

            string orderBy = "order by TKT_NUMBER";
            if (ViewState["sortExpr"] != null)
            {
                orderBy = "order by " + ViewState["sortExpr"] + " " + GetSortDirection();
            }
            listProcessing.DataSource = bus.getByStatus(2, orderBy);
            listProcessing.DataBind();

            if (listProcessing.Rows.Count != 0)
            {
                btnDelete.Visible = true;
                listProcessing.Columns[6].Visible = true;
            }
        }

        protected void listProcessing_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            listProcessing.PageIndex = e.NewPageIndex;
            this.loadData();
        }

        private string GridViewSortDirection
        {
            get { return ViewState["SortDirection"] as string ?? "DESC"; }
            set { ViewState["SortDirection"] = value; }
        }
        private string GetSortDirection()
        {
            switch (GridViewSortDirection)
            {
                case "ASC": GridViewSortDirection = "DESC"; break;
                case "DESC": GridViewSortDirection = "ASC"; break;
            }
            return GridViewSortDirection;
        }

        protected void listProcessing_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["sortExpr"] = e.SortExpression;
            this.loadData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strArrayID = "";
            foreach (GridViewRow rows in listProcessing.Rows)
            {
                CheckBox chk = (CheckBox)rows.FindControl("cbxDelId");

                if (chk.Checked)
                {
                    strArrayID += "'" + ((Label)rows.FindControl("lblTicketId")).Text.ToString() + "',";
                }
            }
            if (strArrayID != "")
            {
                if (bus.delete(strArrayID.Substring(0, strArrayID.LastIndexOf(","))))
                {
                    this.loadData();
                }
            }
        }
    }
}